package com.fox0g.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fox0g.mapper.*;
import com.fox0g.netty.NettyServerHandler;
import com.fox0g.pojo.PageBean;
import com.fox0g.pojo.Sharebike;
import com.fox0g.pojo.Ysjrepeat;
import com.fox0g.service.FarmService;
import com.fox0g.service.YsjrepeatService;
import com.fox0g.websocket.WebSocketHandler;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
* @author 86176
* @description 针对表【ysjrepeat(饮水机)】的数据库操作Service实现
* @createDate 2024-07-06 16:57:16
*/
@Service
public class YsjrepeatServiceImpl  implements YsjrepeatService{
    ArrayList<String> list = new ArrayList<>();
    //    @Autowired
//    ShengxiangService shengxiangService;
    @Autowired
    FarmService farmService;

    @Autowired
    FarmMapper farmMapper;


    @Autowired
    WebSocketHandler webSocketHandler;
    @Autowired
    NettyServerHandler nettyServerHandler;
    @Autowired
    FreshfoodMapper freshfoodMapper;

    @Autowired
    SharebikeMapper sharebikeMapper;

    @Autowired
    YsjrepeatMapper ysjrepeatMapper;


    public static String Meg = "";

    @Override
    public void HandleMessage(String message) throws IOException {


        webSocketHandler.send(message);
//        解析温湿度光照强度数据帧
        //HWV122.0I12TS6T123.3T221.0Z
        if (message.matches("HWt1[0-9]+.?[0-9]t2[0-9]+sw[0-9]+tds1[0-9]+.?[0-9]tds2[0-9]+Z")) {

            String arrnews1[] = message.split("t1");
            String arrnew2[] = arrnews1[1].split("t2");
            String arrnew3[] = arrnew2[1].split("sw");
            String arrnew4[] = arrnew3[1].split("tds1");
            String arrnew5[] = arrnew4[1].split("tds2");
            String arrnew6[] = arrnew5[1].split("Z");
            String V1 = arrnew2[0];  //截取水温
            String I = arrnew3[0];  //加热温度
            String TS = arrnew4[0]; //水位
            String T1 = arrnew5[0]; //水源水质
            String T2 = arrnew6[0]; //饮水水质
            LocalTime now = LocalDateTime.now().toLocalTime(); //获取当前时间
            //向前端发送这些数据帧
            webSocketHandler.send("t1" + V1);
            webSocketHandler.send("t2" + I);
            webSocketHandler.send("tds1" + T1);
            webSocketHandler.send("tds2" + T2);
            webSocketHandler.send("sw" + TS);
            //将解析后的数据设置给pojo类




        }


    }

    @Override
    public PageBean page(Integer page) {
        PageHelper.startPage(page, 5);
        List<Ysjrepeat> ysjrepeats = ysjrepeatMapper.YsjallData();
        Page<Ysjrepeat> p = (Page<Ysjrepeat>) ysjrepeats;
        System.out.println("总记录数" + p.getTotal());
        return new PageBean(p.getTotal(), p.getResult());

    }

//    @Override
//    public PageBean warnpage(Integer page) {
//        PageHelper.startPage(page, 5);
//        List<Ysjwarn> ysjwarns = ysjwarnMapper.YsjwarnallData();
//        Page<Ysjwarn> p = (Page<Ysjwarn>) ysjwarns;
//        System.out.println("总记录数" + p.getTotal());
//        return new PageBean(p.getTotal(), p.getResult());
//
//    }
}




